package demo.test;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import com.visec.hdy.hadoop.fsb.HdfsFSB;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.util.List;

public class Hdfs {
	public static void main(String[] args) throws Exception {
		String uri = "hdfs://172.17.0.2:9000/";
		// Configuration config = new Configuration();
		// config.set("dfs.client.block.write.replace-datanode-on-failure.policy",
		// "NEVER");
		// config.set("dfs.client.block.write.replace-datanode-on-failure.enable",
		// "true");
		// FileSystem fs = FileSystem.get(URI.create(uri), config);

		// 列出hdfs上/user/fkong/目录下的所有文件和目录

		// FileStatus[] statuses = fs.listStatus(new Path("/sb"));
		// for (FileStatus status : statuses) {
		// Path path = status.getPath();
		// System.out.println(status);
		// }

		// fs.create(new Path("/uu"));

		// fs.delete(new Path("/sb"), true);

		// 在hdfs的/user/fkong目录下创建一个文件，并写入一行文本
		// FSDataOutputStream os = fs.append(new Path("/ee.log"));
		// os.write("dfadfafaf!\n".getBytes());
		// os.flush();
		// os.close();

		// 显示在hdfs的/user/fkong下指定文件的内容
		// InputStream is = fs.open(new Path("/ee.log"));
		// IOUtils.copyBytes(is, System.out, 1024, true);
		HdfsFSB h = new HdfsFSB("hdfs://172.17.0.2:9000/");
		
		List<File> listFiles = h.listFiles(new File("sb"));
		//h.delete(new File("sb"),true);
		
		System.out.println(listFiles.size());
		System.out.println(listFiles);
		System.out.println(listFiles.get(0).length());
		
//		FSDataOutputStream create = (FSDataOutputStream) h.create(new File("test.log"));
//		
//		create.write("dfadfadfadfadfadf".getBytes());
//		create.hflush();
//		
//		
//		InputStream open = h.get(new File("/sb/1526456709456"));
//		BufferedReader br = new BufferedReader(new InputStreamReader(open, "utf-8"));
//		String line = br.readLine();
//		int i=0;
//		while (line != null) {
//			System.out.println(line);
//			i++;
//			line = br.readLine();
//		}
//		System.out.println(i);
		
//		br.close();
//		h.close();
//
//		System.out.println("xxxxxxxxxxxxxxxxxxxxxxx");

		// ByteArrayInputStream sbis=new
		// ByteArrayInputStream("aaaaaaa".getBytes());
		// byte[] b=new byte[2048];
		// int read = sbis.read(b);
		// System.out.println("-------------------"+read);
		// System.out.println(new String(b,0,read));

	}
}